package LeetCode.month02.day39;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class No36 {
    public static void main(String[] args) {
        char[][] chars = new char[][]{
                {'5','3','.','.','7','.','.','.','.'},
                {'6','.','.','1','9','5','.','.','.'},
                {'.','9','8','.','.','.','.','6','.'},
                {'8','.','.','.','6','.','.','.','3'},
                {'4','.','.','8','.','3','.','.','1'},
                {'7','.','.','.','2','.','.','.','6'},
                {'.','6','.','.','.','.','2','8','.'},
                {'.','.','.','4','1','9','.','.','5'},
                {'.','.','.','.','8','.','.','7','9'},
        };
        System.out.println(isValidSudoku(chars));
    }
    public static boolean isValidSudoku(char[][] board) {
        List<Set<Character>> list = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            list.add(new HashSet<>());
        }
        for (int i = 0; i < 9; i++) {
            Set<Character> row = new HashSet<>();
            Set<Character> column = new HashSet<>();
            for (int j = 0; j < 9; j++) {
                if(Character.isDigit(board[i][j])&&row.contains(board[i][j])){
                    System.out.println("行重复");
                    System.out.println(row);
                    System.out.println("i = " + i);
                    System.out.println("j = " + j);
                    return false;
                }
                if(Character.isDigit(board[j][i])&&column.contains(board[j][i])){
                    System.out.println("列重复");
                    System.out.println(column);
                    System.out.println("i = " + i);
                    System.out.println("j = " + j);
                    return false;
                }
                if(Character.isDigit(board[i][j])&&list.get(i/3*3+j/3).contains(board[i][j])){
                    System.out.println("块重复");
                    System.out.println(list.get(i/3*3+j/3));
                    System.out.println("i = " + i);
                    System.out.println("j = " + j);
                    return false;
                }
                Set<Character> set = list.get(i/3*3+j/3);
                set.add(board[i][j]);
                list.set(i/3*3+j/3,set);
                column.add(board[j][i]);
                row.add(board[i][j]);
            }
        }
        return true;
    }
}
